#pragma once
#include <string>
#include <vector>
#include <algorithm>
namespace xpam
{
    namespace string
    {
        inline void split(const std::string & str, const std::string & sep, std::vector<std::string> & res, bool with_empty)
        {
            res.clear();
            size_t pos1 = 0;
            while(true)
            {
                size_t pos2 = str.find(sep, pos1);
                if (pos2 == std::string::npos)
                    break;
                if (pos2 - pos1 > 0 || with_empty)
                res.push_back(str.substr(pos1, pos2 - pos1));
                pos1 = pos2 + sep.size();
            }
            if (pos1 < str.size() || (pos1 == str.size() && with_empty))
                res.push_back(str.substr(pos1));
        }

        inline std::string to_lower(const std::string & s)
        {
            std::string s1(s.size(), '\0');
            std::transform(s.begin(), s.end(), s1.begin(), tolower);
            //std::string s1 = s;
            //for (int i = 0; i < (int)s1.size(); i++)
            //{
            //    s1[i] = tolower(s1[i]);
            //}
            return s1;
        }

        inline bool start_with(const std::string & s1, const std::string & s2)
        {
            return s1.size() < s2.size() ? false : s1.substr(0, s2.size()) == s2;
        }

        inline std::string trim(const std::string & s)
        {
            const std::string white_space = " \t\r\n";
            int start = 0;
            int end = (int)s.size() - 1;

            for (; start < (int)s.size(); start++)
            {
                if (white_space.find(s[start]) == std::string::npos)
                    break;
            }
            
            for (; end >= 0; end--)
            {
                if (white_space.find(s[end]) == std::string::npos)
                    break;
            }

            return s.substr(start, end - start + 1);
        }
    }
}